package org.nalai.tools;


import org.ini4j.Config;
import org.ini4j.Ini;
import org.ini4j.Profile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

public class IniTool {

    private static final String ENV_TAG = "env";
    private static final String ENVNAME_TAG = "envName";
    private static final String CONTROLLER_TAG = "controller";

    public static void wrapIniToEnv(String iniPath) throws Exception {

        @SuppressWarnings("ConstantConditions") String filePath = IniTool.class.getClassLoader().getResource(iniPath).getPath(); // 如何获得resources下面的东西

        InputStream is = new FileInputStream(new File(filePath));
        Config cfg = new Config();

        cfg.setMultiSection(true);
        Ini ini = new Ini();
        ini.setConfig(cfg);
        try {
            ini.load(is);

            List<Profile.Section> envs = ini.getAll(ENV_TAG);  // ini.getAll("grouping");

            for (Profile.Section section : envs) {

                System.out.println(section.get(ENVNAME_TAG));

                List<String> controllerList = section.getAll(CONTROLLER_TAG);

                for (String controller : controllerList) {
                    System.out.println(controller);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
